<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<script type="text/html" id="searchShipment_template">
    {{# layui.each(d.parcels, function(index, parcel){ }}
    <div class="parcel-item">
        <p><strong>包裹{{ index + 1 }}</strong> 共{{ parcel.quantity }}件商品</p>
        <div class="layui-form-item" style="display: inline-block">
            <label class="layui-form-label">物流公司：</label>
            <div class="layui-input-inline">
                <select name="parcels[{{ index }}].courierCode" lay-search class="courier-code">
                    <option value="">请选择一个物流公司</option>
                    {{# layui.each(d.couriers, function(index, express){ }}
                        <option value="{{ express.code }}" {{ express.code == parcel.courierCode ? 'selected' : '' }}>{{ express.name }}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="display: inline-block">
            <label class="layui-form-label">快递单号：</label>
            <div class="layui-input-inline">
                <input class="layui-input courier-number" name="parcels[{{ index }}].courierNumber" autocomplete="off" value="{{ parcel.courierNumber }}" />
            </div>
        </div>
        <input type="hidden" name="parcels[{{ index }}].logisticsId" value="{{ parcel.logisticsId }}" />
        <input type="hidden" name="parcels[{{ index }}].courierName" class="courier-name" value="{{ parcel.courierName }}" />
        <input type="hidden" name="parcels[{{ index }}].targetId" value="{{ parcel.targetId }}" />
    </div>
    {{# }); }}
</script>
<script type="text/javascript">
    // 查看发货信息
    function searchShopment(targetId) {
        txz.ajaxRequest({
            method: 'get',
            url: '${ctx}/logistics/logistics/searchShipment.do',
            saveCache: true,
            cacheObj: null,
            params: {
                targetId: targetId
            },
            callBack: function (res) {
                if (res.success) {
                    var content = $("#searchShipment");
                    $(content).html("");
                    var getTpl = $("#searchShipment_template").html();
                    laytpl(getTpl).render(res.data, function (html) {
                        content.append(html);

                        form.render('select');
                    });

                    function yesCb(index) {
                        var flag = true;
                        $(content).find(".parcel-item").each(function (ind, ele) {
                            var courierCode = $(ele).find(".courier-code").find(":selected").val();
                            if (!util.isValid(courierCode)) {
                                util.layerMsgError("请选择物流公司");
                                flag = false;
                                return false;
                            }
                            var courierName = $(ele).find(".courier-code").find(":selected").text();
                            $(content).find(".courier-name").val(courierName);

                            var courierNum = $(ele).find(".courier-number").val().trim();
                            if (!util.isValid(courierNum)) {
                                util.layerMsgError("请填写快递单号");
                                flag = false;
                                return false;
                            }
                        });

                        if (flag) {
                            txz.ajaxRequest({
                                method: 'post',
                                url: '${ctx}/logistics/logistics/updateShipment.do',
                                saveCache: null,
                                cacheObj: null,
                                params: $("#searchShipment").serialize(),
                                callBack: function (res) {
                                    if (res.success) {
                                        util.layerMsgSuccess("修改成功", function () {
                                            location.reload();
                                        });
                                    } else {
                                        util.layerMsgError("修改失败");
                                    }
                                }
                            });
                        }
                    }

                    openDialog('修改物流', '#searchShipment', '800px', '500px', yesCb);
                } else {
                    util.layerMsgError("加载异常");
                }
            }
        });
    }

    /**
     * 确认收货
     * @param orderId 订单id
     */
    function receiveShipment(orderId) {
        layer.confirm('确认要点确认收货吗？', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            $.post("${ctx}/mall/order/receiveShipment.do", {
                "orderId": orderId
            }, function (data) {
                layer.close(loadIndex);
                if (data.success) {
                    util.layerMsgSuccess("操作成功！", function () {
                        window.location.reload();
                    });
                } else {
                    util.layerMsgError(data.description)
                }
                top.layer.close(index);
            });
        });
    }
</script>